<?php
// 4/27/2010: immanuel192 write this action 

if (!defined("EXPONENT")) exit("");
	/* draws a calendar */
	
	echo $_REQUEST['t2'];
	$bible_id=0;

global $db;

function show_bible($bible_id,$data)
{
	global $db;
	$html="<p>";
	// for book name
	$html.="<b>".$data->book_name."</b><br>";
	for ($i=$data->chapter_f;$i<=$data->chapter_t;$i++)
	{
		$html.="&nbsp;<a href='javascript:bible_switch({$bible_id},{$data->book},{$i});'>{$i}</a>&nbsp;";
		if (($i-$data->chapter_f+1) % 3 ==0)
			$html.="<br>";
	}
	$html.="</p>";
	return $html;
}

function draw_calendar($month,$year){
	global $db;
	
	$type=1;
	if (isset($_REQUEST['type']))
		$type=intval($_REQUEST['type']);
	// get bible information
	$bible_id=0;
	if (isset($_REQUEST['bible']))
		$bible_id=intval($_REQUEST['bible']);
	$bible=$db->selectObject("bibles","id = {$bible_id}");
	if ($bible == null) die();
	
	
	// load i18n string
	$lang = exponent_lang_loadFile('modules/biblemodule/bible.php');
	// okies, we get bibles here
	
	$data=$db->selectObjectsBySql("
		SELECT {$db->prefix}bible_oneyear.*, {$db->prefix}bible_info.book_name book_name
		FROM  {$db->prefix}bible_oneyear INNER JOIN {$db->prefix}bible_info
		ON {$db->prefix}bible_oneyear.book = {$db->prefix}bible_info.bindex
		WHERE id = {$type} AND bmonth={$month} ORDER BY bday");
	
	/* draw table */
	$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

	/* table headings */
	$headings = array($lang['sun'],$lang['mon'],$lang['tue'],$lang['wed'],$lang['thur'],$lang['fri'],$lang['sat']);
	$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

	/* days and weeks vars now ... */
	$running_day = date('w',mktime(0,0,0,$month,1,$year));
	$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
	$days_in_this_week = 1;
	$day_counter = 0;
	$dates_array = array();

	/* row for week one */
	$calendar.= '<tr class="calendar-row">';

	/* print "blank" days until the first of the current week */
	for($x = 0; $x < $running_day; $x++):
		$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
		$days_in_this_week++;
	endfor;

	/* keep going with days.... */
	for($list_day = 1; $list_day <= $days_in_month; $list_day++):
		$calendar.= '<td class="calendar-day">';
			/* add in the day number */
			$calendar.= '<div class="day-number">'.$list_day.'</div>';

			/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
			$flag=false;
			foreach ($data as $k=>$v)
				{
					if ($v->bday == $list_day)
						{
							$calendar.=show_bible($bible_id,$v);
							$flag=true;
						}
					if (($v->day != $list_day) && ($flag)) break;
				}
			
		$calendar.= '</td>';
		if($running_day == 6):
			$calendar.= '</tr>';
			if(($day_counter+1) != $days_in_month):
				$calendar.= '<tr class="calendar-row">';
			endif;
			$running_day = -1;
			$days_in_this_week = 0;
		endif;
		$days_in_this_week++; $running_day++; $day_counter++;
	endfor;

	/* finish the rest of the days in the week */
	if($days_in_this_week < 8):
		for($x = 1; $x <= (8 - $days_in_this_week); $x++):
			$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
		endfor;
	endif;

	/* final row */
	$calendar.= '</tr>';

	/* end the table */
	$calendar.= '</table>';
	
	/* all done, return result */
	return $calendar;
}

echo draw_calendar(date("n"),date("Y"));

?>
